package com.cloudys.controller;

import com.cloudys.bean.TestUser;
import com.cloudys.service.TestUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class TestUserController {

    //增加日志
    private final Logger log = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestUserService testUserService;

    @RequestMapping("/{id}")
    public String  getUser(@PathVariable Long id, Model model) {
        //打印日志
        log.info("getUser getUser info"+id);
        model.addAttribute("user",new TestUser(id,"张三",20,"中国广州"));
        return "/testUser/detail";
    }

    @RequestMapping("/list")
    public String  listUser(Model model) {
        List<TestUser> userList = new ArrayList<TestUser>();
        for (long i = 0; i <10; i++) {
            userList.add(new TestUser(i,"张三"+i,20,"中国广州"));
        }

        model.addAttribute("userList", userList);
        return "/testUser/list";
    }


    @RequestMapping("/getUserByName")
    @ResponseBody
    TestUser getUser(String name) {
        //打印日志
        log.info("TestUserController getUser info");
        return testUserService.getTestUserByName(name);
    }

    @RequestMapping("saveUser")
    @ResponseBody
    public TestUser saveUser(TestUser testUser) {//注意 @RequestMapping("saveFruits")里的value值要与方法名称一致，否则找不到

        return testUserService.saveUser(testUser);
    }

}
